import { getEventParam } from '../../utils/utils'
import { setTabBarBadge } from '../../utils/wx'
import Service from '../../model/service'
import cache from '../../enum/cache'
// pages/publish/publish.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      type: null,
      title: '',
      category_id: null,
      cover_image: null,
      description: '',
      designated_place: false,
      begin_date: '',
      end_date: '',
      price: '',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  onShow() {
    const unreadCount = wx.getStorageSync(cache.UNREAD_COUNT)
    setTabBarBadge(unreadCount)
  },
  async handleSubmit(event) {
    const res = await wx.showModal({
      title: '提示',
      content: '是否确认发布服务',
      showCancel: true,
    })
    if (!res.confirm) {
      return
    }
    wx.showLoading({
      title: '正在发布....',
      mask: true,
    })
    const formData = getEventParam(event, 'formData')
    console.log(event)
    try {
      await Service.publishService(formData)

      this._resetForm()
      wx.navigateTo({
        url: `/pages/publisher-success/publisher-success?type=${formData.type}`,
      })
    } catch (error) {
      console.log(error)
    }
    wx.hideLoading()
  },
  _resetForm() {
    const formData = {
      type: null,
      title: '',
      category_id: null,
      cover_image: null,
      description: '',
      designated_place: false,
      begin_date: '',
      end_date: '',
      price: '',
    }
    this.setData({
      formData,
    })
  },
})
